package userctrl

import (
	"net/http"
	"strconv"
	"web_server/model/mainmodel"
	"web_server/utils"

	"github.com/gin-gonic/gin"
)

func UserAdd(ctx *gin.Context, user mainmodel.UserModel) {
	ctx.HTML(http.StatusOK, "user-add.html", gin.H{
		"pubkey": utils.GetPubKeyString(),
	})
}

func UserLst(ctx *gin.Context, user mainmodel.UserModel) {
	if nameCodeStr, ok := ctx.GetQuery("namecode"); ok {
		userlist := mainmodel.GetUserListByCode(nameCodeStr)
		ctx.HTML(http.StatusOK, "user-list.html", gin.H{
			"ul":      userlist,
			"page":    1,
			"pagemax": 1,
		})
		return
	}
	pageStr := ctx.Query("page")
	usernum := mainmodel.GetUserNum()
	pagemax := (usernum + 19) / 20
	var page int64 = 1
	if n, err := strconv.ParseInt(pageStr, 10, 64); err != nil && n > 0 {
		if n <= pagemax {
			page = n
		} else {
			page = pagemax
		}
	}
	userlist := mainmodel.GetUserList(page)
	ctx.HTML(http.StatusOK, "user-list.html", gin.H{
		"ul":      userlist,
		"page":    page,
		"last":    page - 1,
		"next":    page + 1,
		"pagemax": pagemax,
		"pubkey":  utils.GetPubKeyString(),
	})
}

func UserEdt(ctx *gin.Context, user mainmodel.UserModel) {
	editIdStr := ctx.Query("id")
	editId := user.Id
	if id, err := strconv.ParseInt(editIdStr, 10, 64); err == nil && id != 0 {
		editId = id
	}
	if editUser := mainmodel.GetUserModel(editId); editUser != nil {
		ctx.HTML(http.StatusOK, "user-edit.html", gin.H{
			"um":     *editUser,
			"pubkey": utils.GetPubKeyString(),
		})
	} else {
		ctx.String(http.StatusOK, "没有此用户!")
	}
}

func UserAddPost(ctx *gin.Context, user mainmodel.UserModel) {
	r := ctx.Request
	var jsdata mainmodel.UserElemAdd
	if err := utils.B64RsaJsonDecrypt(r.FormValue("data"), &jsdata); err != nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "信息格式错误",
		})
		return
	}
	if len(jsdata.Namecode) < 5 || len(jsdata.Namecode) > 10 ||
		len(jsdata.Username) < 5 || len(jsdata.Username) > 10 ||
		(len(jsdata.Password) != 0 && (len(jsdata.Password) < 5 || len(jsdata.Password) > 15)) ||
		jsdata.Role < 2 || jsdata.Role > 3 {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "信息格式错误",
		})
		return
	}
	if err := mainmodel.SetUserAdd(jsdata); err != nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "创建失败: " + err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"status": true,
	})
}

func UserEdtPost(ctx *gin.Context, user mainmodel.UserModel) {
	r := ctx.Request
	var jsdata mainmodel.UserElemEdit
	if err := utils.B64RsaJsonDecrypt(r.FormValue("data"), &jsdata); err != nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "信息格式错误",
		})
		return
	}
	if len(jsdata.Namecode) < 5 || len(jsdata.Namecode) > 10 ||
		len(jsdata.Username) < 5 || len(jsdata.Username) > 10 ||
		(len(jsdata.Password) != 0 && (len(jsdata.Password) < 5 || len(jsdata.Password) > 15)) ||
		jsdata.Role < 2 || jsdata.Role > 3 {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "信息格式错误",
		})
		return
	}
	if jsdata.Id == 1 {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "admin账户不允许修改密码以外信息! 密码请从个人信息页面修改",
		})
		return
	}
	if err := mainmodel.SetUserEdit(jsdata); err != nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "修改失败: " + err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"status": true,
	})
}

func UserDelPost(ctx *gin.Context, user mainmodel.UserModel) {
	r := ctx.Request
	data := r.FormValue("data")
	var jsdata struct {
		Ids []int `json:"id"`
	}
	if err := utils.B64RsaJsonDecrypt(data, &jsdata); err != nil || len(jsdata.Ids) == 0 || len(jsdata.Ids) >= 20 {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "信息格式错误",
		})
	}
	if err := mainmodel.SetUserDel(jsdata.Ids); err != nil {
		ctx.JSON(0, gin.H{
			"status": false,
			"msg":    "删除失败: " + err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"status": true,
	})
}
